package khleuven.mondzorg.db.writers;


import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.db.DBException;
import khleuven.mondzorg.domain.JsonKeysDictionary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class OfflineSourceWriterGeneral implements OfflineSourceWriter {

	@Override
	public void write(FileOutputStream fos, JSONArray array) throws DBException{
		try {
			JSONObject object = new JSONObject();
			object.put(JsonKeysDictionary.JSON_KEY_GS_ENTRY, array);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
			String toWrite = object.toString();
			Logger.logDB("Will be written:\n"+toWrite);
			out.write(toWrite);
			out.close();
		} catch (IOException e) {
			throw new DBException("Cannot open file for writing",e);
		} catch (JSONException e) {
			throw new DBException("json mistake",e);
		}		
	}

}
